import {findReportById,updateReportScoreById} from "../../../../api/report"
const app = getApp()
const {Toast} = app.globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPopup:false,
    firstRate:0,
    secondRate:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let {id} = options

    let ret = await findReportById(id)
    let newProcessTime = ret.data[0].process.at(-1).time
    newProcessTime = newProcessTime.split(" ")[0]

    if(ret.code === 200){
      this.setData({
        id,
        reportInfo:ret.data[0],
        newProcessTime
      })
    }
  },

  // 跳转至图片预览
  previewImg(e){
    let {url} = e.currentTarget.dataset

    wx.navigateTo({
      url: '/pages/menu/report/imgPreview/index?url='+url,
    })
  },

  // 跳转至上报进展
  toProcess(){
    let {process} = this.data.reportInfo
    process = JSON.stringify(process)

    wx.navigateTo({
      url: '/pages/menu/report/process/index?process='+process,
    })
  },

  // 点击反馈按钮，跳出弹窗，进行打分
  feedback(){
    this.setData({
      showPopup:!this.data.showPopup
    })
  },

  // 上传评分
  async confirm(){
    let {firstRate,secondRate} = this.data
    let addedScore = 0
    if(firstRate == 0 && secondRate == 0){
      Toast.fail("您还未评价")
    }else{
      addedScore = firstRate * 20 + (5-secondRate) * 10
      let ret = await updateReportScoreById(this.data.id,addedScore)
      if(ret.code == 201){
        Toast.success("反馈成功")
        this.setData({
          showPopup:false
        })
      }else{
        Toast.fail("反馈失败")
      }
    }
  }
})